package com.example.wuren;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private EditText phoneEditText;
    private EditText emailEditText;
    private Spinner userTypeSpinner;
    private Button registerButton;
    private String selectedUserType = "";
    private SharedPreferences userPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        // 初始化控件
        usernameEditText = findViewById(R.id.username_edit_text);
        passwordEditText = findViewById(R.id.password_edit_text);
        phoneEditText = findViewById(R.id.phone_edit_text);
        emailEditText = findViewById(R.id.email_edit_text);
        userTypeSpinner = findViewById(R.id.user_type_spinner);
        registerButton = findViewById(R.id.register_button);

        // 初始化SharedPreferences
        userPrefs = getSharedPreferences("UserPrefs", MODE_PRIVATE);

        // 设置用户类型下拉框
        setupUserTypeSpinner();

        // 设置注册按钮点击事件
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });
    }

    private void setupUserTypeSpinner() {
        // 创建用户类型数组
        String[] userTypes = {"求职者", "招聘者"};

        // 创建适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_item,
                userTypes
        );

        // 设置下拉列表的样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // 应用适配器到Spinner
        userTypeSpinner.setAdapter(adapter);

        // 设置选择监听器
        userTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedUserType = userTypes[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 未选择时的处理
            }
        });
    }

    private void registerUser() {
        // 获取输入内容
        String username = usernameEditText.getText().toString().trim();
        String password = passwordEditText.getText().toString().trim();
        String phone = phoneEditText.getText().toString().trim();
        String email = emailEditText.getText().toString().trim();

        // 简单验证
        if (username.isEmpty() || password.isEmpty() || phone.isEmpty() || email.isEmpty()) {
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.length() < 6) {
            Toast.makeText(this, "密码长度至少为6位", Toast.LENGTH_SHORT).show();
            return;
        }

        // 将用户信息保存到SharedPreferences
        SharedPreferences.Editor editor = userPrefs.edit();
        editor.putString(username, password);
        editor.apply();

        // 模拟注册成功（实际应用中需要连接服务器）
        String message = "注册成功！\n" +
                "用户名: " + username + "\n" +
                "用户类型: " + selectedUserType + "\n" +
                "手机号: " + phone + "\n" +
                "邮箱: " + email;

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

        // 注册成功后关闭当前页面
        finish();
    }
}